package ProjectManagement;

import com.spm.pojo.Grade;
import com.spm.pojo.StudentGrade;
import com.spm.pojo.Teacher;
import com.spm.service.GradeService;
import com.spm.service.impl.GradeServiceImpl;
import com.spm.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.ArrayList;
import java.util.List;

@WebServlet(name = "GradeEnter",value = "/GradeEnter")
public class GradeEnter extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
        }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String method = (String) request.getParameter("method");
        if("enter".equals(method)){
            enter(request, response);
        }else {
            GradeEnter(request,response);
        }
    }

    public void enter(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
        String mainPage = "GradeEnter.jsp";
        request.setAttribute("mainPage", mainPage);
        RequestDispatcher dispatcher = request.getRequestDispatcher("/ProjectManagement/main.jsp");
        dispatcher.forward(request, response);
    }
    public void GradeEnter(HttpServletRequest request,
                            HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String sid=request.getParameter("sid");
        String SName=request.getParameter("sName");
        String cClass=request.getParameter("cClass");
        String DailyGrade=request.getParameter("DailyGrade");
        String EndGrade=request.getParameter("EndGrade");
        HttpSession session=request.getSession();
        Teacher teacher=(Teacher)session.getAttribute("teacher") ;
        /*Connection conn=DobConn.getConn();
        int a=0;
        String sql = "update Student_R_Course set DailyScore=? and FinalScore=? where Sid=?";
        try {
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, Integer.parseInt(DailyGrade));
            pstmt.setInt(2, Integer.parseInt(EndGrade));
            pstmt.setString(3,sid);
            a= pstmt.executeUpdate();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }*/
        Grade grade=new Grade((String) session.getAttribute("courseid"),DailyGrade,EndGrade);
        StudentGrade studentGrade=new StudentGrade(sid,SName,cClass,grade);
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        GradeService gradeService = new GradeServiceImpl(sqlSession);
        int i=gradeService.uploadOneGrade(studentGrade);
        List<StudentGrade> list = new ArrayList<StudentGrade>();
        list.add(studentGrade);
        gradeService.calculateScore(list);
        String mainPage = "coursestudent.jsp";
        request.setAttribute("mainPage", mainPage);
        RequestDispatcher dispatcher = request.getRequestDispatcher("/ProjectManagement/main.jsp");
        dispatcher.forward(request, response);
    }
}
